<template>
<div class="container__">
    <van-cell-group>
        <van-field :password="true" v-if="isSetPasswod" label="旧密码" :value="password.old_pay_password" type="password" placeholder="请输入密码" :maxlength="6" @change="oldpaychange" />
        <van-field :password="true" label="密码" :value="password.pay_pass" type="password" placeholder="请输入密码" :maxlength="6" @change="paychange" />
        <van-field :password="true" label="确认密码" :value="password.confirm_pass" type="password" placeholder="请再次输入密码" @change="paychange1" :maxlength="6" />
    </van-cell-group>
    <div class="tip">
        <!-- <span v-if="type == 'login'"> 提示：密码必须至少8个字符，同时包含字母和数字</span> -->
        <span> 提示：密码必须6个数字字符</span>
    </div>
    <van-cell class="cnter" @click="sbmit">
        保存
    </van-cell>
</div>
</template>

<script>
import {
    password
} from '@/api/my'

import {
    checkNumber
} from '@/utils/index'
export default {
    data() {
        return {
            password: {
                pay_pass: '',
                confirm_pass: '',
            },
            queren: '',
            isSetPasswod: '',
        }
    },
    methods: {
        sbmit() {
            if (!this.password.pay_pass) return
            if (this.password.pay_pass.length < 6) {
                wx.showToast({
                    title: '密码不能小于6',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            if (!checkNumber(this.password.pay_pass)) {
                wx.showToast({
                    title: '密码只能为数字',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            if (this.password.pay_pass != this.password.confirm_pass) {
                wx.showToast({
                    title: '两次密码不一致',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
            password(this.password).then(res => {
                 if(res.code==200){
                     wx.showToast({
                         title:"设置密码成功",
                         success:res=>{
                             wx.navigateBack({
                                delta: 1
                            });
                         }
                     })
                 }else if(res.code==201){
                     wx.showToast({
                         title:res.msg
                     })
                 }
            })
        },
        paychange(el) {
            this.password.pay_pass = el.mp.detail
        },
        oldpaychange(el) {
            this.password.old_pay_password = el.mp.detail
        },
        paychange1(el) {
            this.password.confirm_pass = el.mp.detail
        },
    },
    mounted() {
        if (!wx.getStorageSync('user_info').PayPassword) return
        this.isSetPasswod = wx.getStorageSync('user_info').PayPassword
    },
    onLoad() {
        Object.assign(this.$data, this.$options.data())
    }

}
</script>

<style lang="scss">
.container__ {
    background-color: #F4F5F5;
    min-height: 100vh;
    font-size: 16px;
    // padding: 40px 15px;
    box-sizing: border-box;
    .tip {
        font-size: 14px;
        font-weight: 500;
        color: rgba(102, 102, 102, 1);
        box-sizing: border-box;
        padding: 0 15px;
        margin-bottom: 40px;
    }
    .van-cell {
        align-items: center;
        height: 50px;
        font-size: 15px;
    }
    .van-field__input {
        font-size: 14px;
        text-align: right;
    }
    .van-cell-group {
        margin-bottom: 10px;
    }
    .cnter {
        .van-cell__value {
            font-size: 14px;
            font-weight: 500;
            color: rgba(51, 51, 51, 1);
            text-align: center;
        }
    }
}
</style>
